package cn.edu.scau.cmi.mingjunhui.client;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.mingjunhui.adapter.AdapterOfClass;
import cn.edu.scau.cmi.mingjunhui.adapter.AdapterOfObject;
import cn.edu.scau.cmi.mingjunhui.adapter.SortStudentUtil;
import cn.edu.scau.cmi.mingjunhui.adapter.Student;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class AdapterClient extends Application {

	List<Student> studentList = new ArrayList<>(); 
	
   @Override
   public void start(Stage primaryStage) {
	   VBox root = new VBox(10);
	   adapterTest0(root);
       
       Scene scene = new Scene(root, 300, 500);
       primaryStage.setTitle("适配器模式");
       primaryStage.setScene(scene);
       primaryStage.show();
   }

   public void adapterTest0(Pane pane) {
       Button btn0 = new Button();
       btn0.setText("类适配器");
       btn0.setOnAction(event -> {
    	   studentList.clear();
    	   classAdapter(pane);
       });
       pane.getChildren().add(btn0);
       
       Button btn1 = new Button();
       btn1.setText("对象适配器");
       btn1.setOnAction(event -> {
    	   studentList.clear();
    	   objectAdapter(pane);
       });
       pane.getChildren().add(btn1);
       
       classAdapter(pane);
   }
   
   public void classAdapter(Pane pane) {
	   pane.getChildren().remove(2, pane.getChildren().size());
	   Text title = new Text("");
	   pane.getChildren().add(title);
	   
	   TextField idTextField = new TextField();
	   Label idLabel = new Label("学号");
	   pane.getChildren().addAll(idLabel, idTextField);
	   
	   TextField nameTextField = new TextField();
	   Label nameLabel = new Label("名字");
	   pane.getChildren().addAll(nameLabel, nameTextField);
	   
	   Button btn = new Button();
       btn.setText("添加");
       btn.setOnAction(event -> {
    	   Student student = new Student(Integer.valueOf(idTextField.getText()), nameTextField.getText());
    	   SortStudentUtil sortStudentUtil = new AdapterOfClass();
    	   studentList.add(student);
    	   this.studentList = sortStudentUtil.sort(studentList);
    	   pane.getChildren().remove(8, pane.getChildren().size());
    	   for (Student student0 : studentList) {
    		   Text t = new Text();
    		   t.setText(student0.toString());
    		   pane.getChildren().add(t);
    	   }
       });
 
       pane.getChildren().add(btn);
   }
	
   public void objectAdapter(Pane pane) {
	   pane.getChildren().remove(2, pane.getChildren().size());
	   Text title = new Text("");
	   pane.getChildren().add(title);
	   
	   TextField idTextField = new TextField();
	   Label idLabel = new Label("学号");
	   pane.getChildren().addAll(idLabel, idTextField);
	   
	   TextField nameTextField = new TextField();
	   Label nameLabel = new Label("名字");
	   pane.getChildren().addAll(nameLabel, nameTextField);
	   
	   Button btn = new Button();
       btn.setText("添加");
       btn.setOnAction(event -> {
    	   Student student = new Student(Integer.valueOf(idTextField.getText()), nameTextField.getText());
    	   SortStudentUtil sortStudentUtil = new AdapterOfObject();
    	   studentList.add(student);
    	   this.studentList = sortStudentUtil.sort(studentList);
    	   pane.getChildren().remove(8, pane.getChildren().size());
    	   for (Student student0 : studentList) {
    		   Text t = new Text();
    		   t.setText(student0.toString());
    		   pane.getChildren().add(t);
    	   }
       });
 
       pane.getChildren().add(btn);
   }
   
	public static void main(String[] args) {
		launch(args);
	}
}